﻿using System;
using Enums;
using XF.ExControls;
using XF.Common;
using System.Windows.Forms;

namespace KYBM
{
    public partial class FrmDialogInit : XFFormEx
    {
        readonly XF.BLL.Base_Customer bllCustomer = new XF.BLL.Base_Customer();
        public XF.Model.Base_CheckOut CheckOut { get; set; } = new XF.Model.Base_CheckOut();

        public FrmDialogInit()
        {
            InitializeComponent();
        }

        public FrmDialogInit(int customerId)
        {
            InitializeComponent();
            XF.Model.Base_Customer customer = bllCustomer.GetModel(customerId);
            if(customer != null)
            {
                CheckOut.CustomerId = customer.Id;
                CheckOut.CustomerCode = customer.CustomerCode;
                CheckOut.CustomerName = customer.CustomerName;
            }
        }

        private void tsBtnSure_Click(object sender, EventArgs e)
        {
            decimal arrears;
            if(!decimal.TryParse(tbArrears.Text,out arrears))
            {
                QQMessageBox.Show(
                            this,
                            MessageText.CHECK_ERROR_ARREARS,
                            MessageText.MESSAGEBOX_CAPTION_TIP,
                            QQMessageBoxIcon.Information,
                            QQMessageBoxButtons.OK);
                return;
            }
            CheckOut.CheckOutDate = dtpCheckOutDate.Value;
            CheckOut.Totle = arrears;
            CheckOut.Arrears = arrears;
            CheckOut.Remark = tbRemark.Text;
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void FrmDialogCheckOut_Load(object sender, EventArgs e)
        {
            tbCode.Text = CheckOut.CustomerCode;
            tbName.Text = CheckOut.CustomerName;
            tbArrears.Text = CheckOut.Arrears.ToString("0.######");
            dtpCheckOutDate.Value = DateTime.Now.Date;
        }

        private void tsBtnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FrmDialogInit_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Enter:
                    tsBtnSure_Click(this, EventArgs.Empty);
                    break;
                case Keys.Escape:
                    tsBtnCancel_Click(this, EventArgs.Empty);
                    break;
            }
        }
    }
}
